//
//  KWTMGRNSettingModel.m
//  RuiHeApp
//
//  Created by Yan Chen on 2024/7/29.
//

#import "KWTMGRNSettingModel.h"

@implementation KWTMGRNSettingModel
+ (nullable NSDictionary<NSString *, id> *)modelCustomPropertyMapper
{
    return @{@"num":@"bitWidth"};
}

+ (NSDictionary *)modelContainerPropertyGenericClass {
    return @{@"rows" : [KWTMGRNSettingModel class],
             @"items" : [KWTMGRNSettingModel class],
             @"selectList":[DevSettingOptionModel class]};
 }

- (void)readDefult:(NSDictionary *)dataDic
{
    NSMutableDictionary *dic = dataDic.mutableCopy;
    for (int i = 0; i< 6; i++) {
        //找到SOC和功率的key,如果这两个都有值,且为0,则是待机,设为2
        NSString *soc_key = [NSString stringWithFormat:@"Inv3_TSoc%d",i+1];
        NSString *power_key = [NSString stringWithFormat:@"Inv3_TPo%d",i+1];
        NSString *T_EN_key = [NSString stringWithFormat:@"Inv3_T%dEn",i+1];
        NSString *Gen_EN_key = [NSString stringWithFormat:@"Inv3_Gen%dEn",i+1];
        //有值才处理  两个使能都为0时,就是待机
        if(dic[T_EN_key]&&dic[Gen_EN_key]){
            if([dic[T_EN_key] integerValue] == 0 && [dic[Gen_EN_key] integerValue] == 0){
                //充电使能设为2,用于显示
                [dic setObject:@"2" forKey:T_EN_key];
            }
        }
    }
    
    
    [self.rows enumerateObjectsUsingBlock:^(KWTMGRNSettingModel * _Nonnull rowModel, NSUInteger row_idx, BOOL * _Nonnull stop) {
        //时间校准
        if ([rowModel.type isEqualToString:@"date"]) {
            rowModel.content = [NSString stringWithFormat:@"20%02ld/%02ld/%02ld %02ld:%02ld:%02ld",[dic[@"Inv3_RTCYear"] integerValue],[dic[@"Inv3_RTCMonth"] integerValue],[dic[@"Inv3_RTCDay"] integerValue],[dic[@"Inv3_RTCHour"] integerValue],[dic[@"Inv3_RTCMinute"] integerValue],[dic[@"Inv3_RTCSecond"] integerValue]];
            return;
        }
        //充放电时间设置
        if ([rowModel.type isEqualToString:@"time"]) {
            [rowModel.items enumerateObjectsUsingBlock:^(KWTMGRNSettingModel * _Nonnull itemModel, NSUInteger item_idx, BOOL * _Nonnull stop) {
                NSLog(@"---->key:%@   value:%@",itemModel.key,dic[itemModel.key]);
               
                if ([itemModel.type isEqualToString:@"select"]) {
                    //下标值
                    for (DevSettingOptionModel *select_model in itemModel.selectList) {
                        //有时才处理
                        if(([dic[itemModel.key] integerValue] == select_model.textValue)&&dic[itemModel.key]){
                            itemModel.enumText = select_model.text;
                            itemModel.content = [NSString stringWithFormat:@"%ld",select_model.textValue];
                        }
                    }
                }
                //充放电开始时间和结束时间
                if ([itemModel.type isEqualToString:@"date"] && [dic[itemModel.key] length] != 0) {
                    //小时
                    NSInteger hour = [dic[itemModel.key] integerValue]/100;
                    //分钟
                    NSInteger minute = [dic[itemModel.key] integerValue]%100;
                    itemModel.content = [NSString stringWithFormat:@"%02ld:%02ld",hour,minute];//
                  
                }
                if ([itemModel.type isEqualToString:@"switch"]){
                    itemModel.content = ValidNullData(dic[itemModel.key]);
                }
                if ([itemModel.type isEqualToString:@"number"]){
                    itemModel.content = ValidNullData(dic[itemModel.key]);
                }
               
               
            }];
            return;
        }
        
        
        
        //开关和数值输入
        NSLog(@"key:%@   value:%@",rowModel.key,dic[rowModel.key]);
        rowModel.content = ValidNullData(dic[rowModel.key]);
        
        //选择
        if ([rowModel.type isEqualToString:@"select"]) {
            //下标值
            for (DevSettingOptionModel *select_model in rowModel.selectList) {
                if([dic[rowModel.key] integerValue] == select_model.textValue){
                    rowModel.enumText = select_model.text;
                }
            }
        }
    }];
}
@end
